package com.landking.home;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.AppCompatTextView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.landking.R;
import com.landking.base.BaseMainFragment;
import com.landking.db.table.LK_BANNER_M;
import com.landking.view.convenientbanner.BannerCreator;

import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;


/**
 *
 */
public class HotFragment extends BaseMainFragment implements View.OnClickListener {

    /**
     * UI
     */
    private View mContentView;
    private FrameLayout containerFl;
    private RelativeLayout backBtn;
    private RelativeLayout confirmBtn;
    private AppCompatTextView confirmTv;
    private AppCompatTextView backTv;
    private AppCompatTextView titleTv;
    ConvenientBanner<String> convenientBanner;


    String title = "最火";// 需要改

    private static final RequestOptions BANNER_OPTIONS = new RequestOptions()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .dontAnimate()
            .centerCrop();


    public static HotFragment newInstance() {
        Bundle args = new Bundle();
        HotFragment fragment = new HotFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public static HotFragment newInstance(Bundle bundle) {
        Bundle args = bundle;
        HotFragment fragment = new HotFragment();
        fragment.setArguments(args);
        return fragment;
    }

    MyHandler handler;

    /**
     * 接收子线程消息的 Handler
     */
    public static class MyHandler extends Handler {

        // 软引用
        SoftReference<HotFragment> fragmentRef;

        public MyHandler(HotFragment fragment) {
            fragmentRef = new SoftReference<HotFragment>(fragment);
        }

        @Override
        public void handleMessage(Message msg) {
            HotFragment fragment = fragmentRef.get();
            if (fragment == null) {
                return;
            }
            Bundle bundle = msg.getData();
            // 处理UI 变化
            switch (msg.what) {
                case 0:
                    //fragment.showAddProSuc(bundle);
                    break;
                case 1:
                    // fragment.showAdapter(bundle);
                    break;
                case 2:
                    // fragment.showTzAdapter(bundle);
                    break;
            }
        }
    }

    /**
     * data
     */
    public HotFragment() {
    }

    // 获取传递过来的参数
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();
        if (arguments != null) {
            title = (String) arguments.get("title");// 需要改
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mContext = getActivity();
        mContentView = inflater.inflate(R.layout.fragment_hot_layout, container, false);// 需要改
        this.initView();
        this.initData();
        return mContentView;
    }

    // 初始化视图
    private void initView() {
        containerFl = (FrameLayout) mContentView.findViewById(R.id.hot_container);// 需要改
        backBtn = (RelativeLayout) mContentView.findViewById(R.id.top_navigation_rl_back);
        confirmBtn = (RelativeLayout) mContentView.findViewById(R.id.top_navigation_rl_confirm);
        confirmTv = (AppCompatTextView) mContentView.findViewById(R.id.top_navigation_bt_confirm);
        backTv = (AppCompatTextView) mContentView.findViewById(R.id.top_navigation_bt_back);
        titleTv = (AppCompatTextView) mContentView.findViewById(R.id.top_navigation_tv_title);
        backBtn.setVisibility(View.INVISIBLE);
        confirmBtn.setVisibility(View.INVISIBLE);
        backBtn.setOnClickListener(this);
        confirmBtn.setOnClickListener(this);

        convenientBanner = mContentView.findViewById(R.id.yd_fir_banner_recycler_item);
    }

    // 初始化数据
    public void initData() {
        titleTv.setText("最火");// 需要改

        // 展示banner
        showBanner();

    }

    // 展示banner
    private void showBanner() {
        // BannerStc
        BmobQuery<LK_BANNER_M> categoryBmobQuery = new BmobQuery<>();
        categoryBmobQuery.addWhereEqualTo("pictype", "banner");
        categoryBmobQuery.findObjects(new FindListener<LK_BANNER_M>() {
            @Override
            public void done(List<LK_BANNER_M> object, BmobException e) {
                if (e == null) {// 查询成功
                    ArrayList<String> images = new ArrayList<>();
                    for (LK_BANNER_M banner : object) {
                        if("1".equals(banner.getStatus())){
                            images.add(banner.getPicurl());
                        }
                    }
                    BannerCreator.setDefault(convenientBanner, images, new OnItemClickListener() {
                        @Override
                        public void onItemClick(int position) {
                            // 条目点击监听
                            // Toast.makeText(_mActivity, "点击了第" + position + "项", Toast.LENGTH_SHORT).show();
                        }
                    });

                } else {// 查询成功
                    Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.top_navigation_rl_back://  // 需要改

                break;
        }
    }
}